#include "common.hpp"
#include "fifo.hpp"

int main()
{
    //得到创建共享内存的key
    key_t client_key = get_shmkey();
    std::cout << "client_key" << to_hexa(client_key) << std::endl;

    //拿到已经建好了的共享内存
    int shm_id = get_shmid(client_key, SHM_SIZE);
    std::cout << "shm_id: " << shm_id << std::endl;

    //共享内存关联进程
    char* address = (char*)shmattch(shm_id);
    std::cout << "Attch shm succsss, addr: " << to_hexa(uint64_t(address)) << std::endl;

    //引入协同机制
    Sync sync;
    sync.open_write();

    //开始通信
    int index = 0;
    for (char ch = 'A'; ch <= 'Z'; ++ch)
    {
        address[index++] = ch;
        sleep(1);
        sync.wakeup();
    }
    
    //取消共享内存和进程的关联
    shm_unattch(address);
    std::cout << "Shm unattch success, address: " << to_hexa(uint64_t(address)) << std::endl;

    return 0;
}